package com.qulai.amazon_us.service.basic.impl;

import java.lang.reflect.Field;
import java.util.*;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.qulai.amazon_us.domain.basic.Image;
import com.qulai.amazon_us.domain.vo.IfExistImageToSkuVo;
import com.qulai.amazon_us.domain.vo.ImageToSkuVo;
import com.qulai.amazon_us.service.basic.IImageService;
import com.qulai.amazon_us.util.basic.BasicUtil;
import com.qulai.common.exception.ServiceException;
import com.qulai.common.utils.DateUtils;
import com.qulai.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qulai.amazon_us.mapper.basic.ImageToSkuMapper;
import com.qulai.amazon_us.domain.basic.ImageToSku;
import com.qulai.amazon_us.service.basic.IImageToSkuService;
import com.qulai.amazon_us.domain.basic.Product;
import com.qulai.amazon_us.service.basic.IProductService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 图片生成skuService业务层处理
 * 
 * @author hunan
 * @date 2025-08-06
 */
@Service
public class ImageToSkuServiceImpl implements IImageToSkuService 
{
    @Autowired
    private ImageToSkuMapper imageToSkuMapper;

    @Autowired
    private IProductService productService;
    @Autowired
    private IImageService iImageService;

    /**
     * 查询图片生成sku
     * 
     * @param id 图片生成sku主键
     * @return 图片生成sku
     */
    @Override
    public ImageToSku selectImageToSkuById(Long id)
    {
        return imageToSkuMapper.selectImageToSkuById(id);
    }

    /**
     * 查询图片生成sku列表
     * 
     * @param imageToSku 图片生成sku
     * @return 图片生成sku
     */
    @Override
    public List<ImageToSku> selectImageToSkuList(ImageToSku imageToSku)
    {
        return imageToSkuMapper.selectImageToSkuList(imageToSku);
    }

    /**
     * 新增图片生成sku
     * 
     * @param imageToSku 图片生成sku
     * @return 结果
     */
    @Override
    @Transactional
    public int insertImageToSku(List<ImageToSku> imageToSkuList,String username)
    {
        int row = 0;
        Date date = DateUtils.getNowDate();
        Long accountId = SecurityUtils.getUserId();

        Long baseProudctId =  imageToSkuList.get(0).getBaseProductId();

        int imageIndexInit = imageToSkuMapper.selectMainImageToSkuListByBaseProductId(baseProudctId).size();

        if(imageIndexInit == 0){

            ImageToSku imageToSkuInit = new ImageToSku();
            Product basicProductInit = productService.selectProductById(baseProudctId.toString());

            imageToSkuInit.setMainImage(BasicUtil.truncateAtQuestionMark(basicProductInit.getMainImage()));
            imageToSkuInit.setCreateTime(date);
            imageToSkuInit.setBaseProductId(Long.parseLong(basicProductInit.getId()));
            imageToSkuInit.setProductId(Long.parseLong(basicProductInit.getId()));
            imageToSkuInit.setCreateUserId(accountId.toString());
            imageToSkuInit.setCreateTime(DateUtils.getNowDate());
            imageToSkuInit.setImageIndex(1);
            imageToSkuInit.setSku(basicProductInit.getSku());
            imageToSkuMapper.insertImageToSku(imageToSkuInit);
        }

        for (ImageToSku imageToSku : imageToSkuList){
            Product basicProduct = productService.selectProductById(imageToSku.getBaseProductId().toString());
            if(Objects.isNull(basicProduct)){
                throw new ServiceException("不存在该SKU！");
            }
            if (Objects.isNull(basicProduct.getImageIndex())){
                throw new ServiceException("该SKU不是原始SKU！");
            }
            if(basicProduct.getImageIndex() != 0){
                throw new ServiceException("该SKU不是原始SKU！");
            }
            if(basicProduct.getProductType().isEmpty() || basicProduct.getProductType().equals("") || basicProduct.getProductType() == null){
                throw new ServiceException("该SKU未更新product_type！");
            }

            if(basicProduct.getProductDealStatus() !=1){
                throw new ServiceException("该SKU未更新product_type！");
            }

            /*查看图片序号*/
            int imageIndex = imageToSkuMapper.selectMainImageToSkuListByBaseProductId(Long.valueOf(basicProduct.getId())).size();

            imageToSku.setMainImage(BasicUtil.truncateAtQuestionMark(imageToSku.getMainImage()));
            imageToSku.setCreateTime(date);
            imageToSku.setBaseProductId(Long.parseLong(basicProduct.getId()));
            imageToSku.setProductId(Long.parseLong(basicProduct.getId()));
            imageToSku.setCreateUserId(accountId.toString());
            imageToSku.setCreateTime(DateUtils.getNowDate());
            imageToSku.setImageIndex(imageIndex+1);
            row += imageToSkuMapper.insertImageToSku(imageToSku);

        }
        return row;
    }

    /**
     * 修改图片生成sku
     * 
     * @param imageToSku 图片生成sku
     * @return 结果
     */
    @Override
    public int updateImageToSku(ImageToSku imageToSku)
    {
        return imageToSkuMapper.updateImageToSku(imageToSku);
    }

    /**
     * 批量删除图片生成sku
     * 
     * @param ids 需要删除的图片生成sku主键
     * @return 结果
     */
    @Override
    public int deleteImageToSkuByIds(Long[] ids)
    {
        return imageToSkuMapper.deleteImageToSkuByIds(ids);
    }

    /**
     * 删除图片生成sku信息
     * 
     * @param id 图片生成sku主键
     * @return 结果
     */
    @Override
    public int deleteImageToSkuById(Long id)
    {
        return imageToSkuMapper.deleteImageToSkuById(id);
    }

    @Override
    public List<ImageToSku> selectImageToSkuListByBaseProductId(Long baseProductId) {
        return imageToSkuMapper.selectImageToSkuListByBaseProductId(baseProductId);
    }

    @Override
    public List<ImageToSkuVo> selectImageToSkuVoList(ImageToSku imageToSku) {
        return imageToSkuMapper.selectImageToSkuVoList(imageToSku);
    }

    @Override
    public List<IfExistImageToSkuVo> IfExistImageToSku(String[] imageUrl) {
        List<IfExistImageToSkuVo> voList = new ArrayList<>();
        for (String url : imageUrl){
            if (!Objects.equals(url, "")){
                IfExistImageToSkuVo vo = new IfExistImageToSkuVo();
                vo.setImageUrl(url);
                String uil = BasicUtil.truncateAtQuestionMark(url);
                vo.setIfExist(imageToSkuMapper.ifExistImageToSku(uil,0) != 0);
                vo.setBackgroundExist(imageToSkuMapper.ifExistImageToSku(uil,1) != 0);
                voList.add(vo);
            }
        }
        return  voList;
    }

    @Override
    public int insertBackgroundImage(List<ImageToSku> imageToSkuList) {
        int row = 0;
        Date date = DateUtils.getNowDate();
        Long accountId = SecurityUtils.getUserId();
        for (ImageToSku imageToSku : imageToSkuList) {
            Product basicProduct = productService.selectProductById(imageToSku.getBaseProductId().toString());
            imageToSku.setMainImage(BasicUtil.truncateAtQuestionMark(imageToSku.getMainImage()));
            imageToSku.setCreateTime(date);
            imageToSku.setBaseProductId(Long.parseLong(basicProduct.getId()));
            imageToSku.setBackgroundFlag(1);
            imageToSku.setProductId(Long.parseLong(basicProduct.getId()));
            imageToSku.setCreateUserId(accountId.toString());
            imageToSku.setCreateTime(DateUtils.getNowDate());
            row += imageToSkuMapper.insertImageToSku(imageToSku);
        }
        return row;
    }
}
